home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / script.lzh / script_0.4 / script_library.c < prev    next >
C/C++ Source or Header  |  1996-12-20  |  6KB  |  241 lines

  1. /*
  2.    script_library.c
  3.  
  4.    bf 11-28-96
  5. */
  6.  
  7. /// Includes
  8. #include <string.h>
  9. #include <exec/memory.h>
  10. #include <rexx/rxslib.h>
  11. #include <rexx/storage.h>
  12. #include <proto/rexxsyslib.h>
  13. #include <proto/exec.h>
  14.  
  15. #if defined (__GNUC__)
  16. #include <stabs.h>
  17. #endif
  18.  
  19. #include "script_library.h"
  20. #include "variables.h"
  21. ///
  22.  
  23. /// Declarations
  24. struct ScriptContext *AllocContext (void);
  25. void FreeContext (struct ScriptContext *sc);
  26.  
  27. struct ExecBase *SysBase;
  28. struct Library *ScriptBase;
  29.  
  30. #if defined (__GNUC__)
  31. const BYTE LibName[] = "script.library";
  32. const BYTE LibIdString[] = "$VER: script.library 0.4 (12-20-96)";
  33. const UWORD LibVersion = 0;
  34. const UWORD LibRevision = 4;
  35. #endif
  36.  
  37. #if defined (__GNUC__)
  38. #define LIBRT
  39. #define REG(regname)
  40. #define STRUCT_MYLIB struct Library
  41. #elif defined (__SASC)
  42. #define LIBRT __saveds __asm
  43. #define REG(regname) register __ ## regname
  44. #define ADDTABL_1(name,arg1);
  45. #define ADDTABL_2(name,arg1,arg2);
  46. #define ADDTABL_3(name,arg1,arg2,arg3);
  47. #define ADDTABL_END();
  48. #define STRUCT_MYLIB struct MyLibrary
  49. #endif
  50. ///
  51.  
  52. /// Library (De)Initialization
  53. int LIBRT
  54. __UserLibInit (REG(a6) STRUCT_MYLIB *libbase)
  55. {
  56.   SysBase = *(struct ExecBase **)4;
  57.   ScriptBase = (struct Library *) libbase;
  58.   return 0; /* success */
  59. }
  60.  
  61. void LIBRT
  62. __UserLibCleanup (REG(a6) STRUCT_MYLIB *libbase)
  63. {
  64. }
  65. ///
  66.  
  67. ADDTABL_3(LIBScript_SetRexxVar,a0,a1,a2);
  68.  
  69. /// Script_SetRexxVar()
  70. /*
  71.    Script_SetRexxVar() is compatible with SetRexxVar():
  72.  
  73.    RESULTS
  74.         error            0 for success, otherwise an error code.
  75.                          (Other codes may exist, these are documented)
  76.                          3 = Insufficient Storage
  77.                          9 = String too long
  78.                         10 = Invalid message
  79.  
  80.    NOTE
  81.         The rm_avail field of a RexxMsg bust be zeroed or point to
  82.         a ScriptContext structure allocated by Script_AllocContext().
  83. */
  84. LONG LIBRT
  85. LIBScript_SetRexxVar (REG(a0) struct RexxMsg *msg, REG(a1) char *name, REG(a2) char *value)
  86. {
  87.   struct ScriptContext *sc;
  88.   LONG err;
  89.  
  90.   if (CheckRexxMsg (msg))
  91.   {
  92.     // Without this CheckRexxMsg() SetRexxVar() causes Enforcer Hits at this point!
  93.     // This is weird since the docs say SetRexxVar() does a CheckRexxMsg() on msg.
  94.     //
  95.     if ((err = SetRexxVar ((struct Message *) msg, name, value, strlen (value))) != 10)
  96.       return err;
  97.   }
  98.   if (sc = (struct ScriptContext *) msg -> rm_avail)
  99.   {
  100.     if (SetStringVariable (sc, name, value))
  101.       return 0;
  102.     return 3;
  103.   }
  104.   return 10;
  105. }
  106. ///
  107.  
  108. ADDTABL_3(LIBScript_GetRexxVar,a0,a1,a2);
  109.  
  110. /// Script_GetRexxVar()
  111. /*
  112.    Script_GetRexxVar() is compatible with GetRexxVar():
  113.  
  114.    RESULTS
  115.         error            0 for success, otherwise an error code.
  116.                          (Other codes may exist, these are documented)
  117.                          3 = Insufficient Storage
  118.                          9 = String too long
  119.                         10 = Invalid message
  120.    NOTE
  121.         The rm_avail field of a RexxMsg bust be zeroed or point to
  122.         a ScriptContext structure allocated by Script_AllocContext().
  123. */
  124. LONG LIBRT
  125. LIBScript_GetRexxVar (REG(a0) struct RexxMsg *msg, REG(a1) char *name, REG(a2) char **value)
  126. {
  127.   struct ScriptContext *sc;
  128.   LONG err;
  129.  
  130.   *value = NULL;
  131.  
  132.   if ((err = GetRexxVar ((struct Message *) msg, name, (UBYTE **) value)) != 10)
  133.     return err;
  134.  
  135.   if (sc = (struct ScriptContext *) msg -> rm_avail)
  136.   {
  137.     *value = GetStringVariable (sc, name);
  138.     return 0;
  139.   }
  140.   return 10;
  141. }
  142. ///
  143.  
  144. ADDTABL_1(LIBScript_AllocContext,a0);
  145.  
  146. /// Script_AllocContext()
  147. struct ScriptContext * LIBRT
  148. LIBScript_AllocContext (void)
  149. {
  150.   return AllocContext ();
  151. }
  152.  
  153. struct ScriptContext *
  154. AllocContext (void)
  155. {
  156.   struct ScriptContext *sc = NULL;
  157.   void *pool = NULL;
  158.  
  159.   if (!(pool = CreatePool (MEMF_PUBLIC, 1024, 1024)))
  160.     return NULL;
  161.  
  162.   if (!(sc = AllocPooled (pool, sizeof (struct ScriptContext))))
  163.   {
  164.     DeletePool (pool);
  165.     return NULL;
  166.   }
  167.   NewList ((struct List *) &sc -> sc_Vars);
  168.   sc -> sc_MemPool = pool;
  169.   return sc;
  170. }
  171. ///
  172.  
  173. ADDTABL_1(LIBScript_FreeContext,a0);
  174.  
  175. /// Script_FreeContext()
  176. void LIBRT
  177. LIBScript_FreeContext (REG(a0) struct ScriptContext *sc)
  178. {
  179.   FreeContext (sc);
  180. }
  181.  
  182. void
  183. FreeContext (struct ScriptContext *sc)
  184. {
  185.   DeletePool (sc -> sc_MemPool);
  186. }
  187. ///
  188.  
  189. ADDTABL_2(LIBScript_SetMsgContext,a0,a1);
  190.  
  191. /// Script_SetMsgContext()
  192. /*
  193.    It may appear silly to have an extra function just to make this assignment but
  194.    rm_avail is not officially reserved to contain "struct ScriptContext *" and
  195.    future versions of script.library may have to use a different method.
  196.    Maybe "msg -> rm_Node.mn_Node.ln_Name" or "msg -> rm_ScriptContext" :)
  197.  
  198.    Please use Script_SetMsgContext() and don't access rm_avail directly!
  199. */
  200. void LIBRT
  201. LIBScript_SetMsgContext (REG(a0) struct RexxMsg *msg, REG(a1) struct ScriptContext *sc)
  202. {
  203.   msg -> rm_avail = (long) sc;
  204. }
  205. ///
  206.  
  207. ADDTABL_1(LIBScript_GetMsgContext,a0);
  208.  
  209. /// Script_GetMsgContext()
  210. struct ScriptContext * LIBRT
  211. LIBScript_GetMsgContext (REG(a0) struct RexxMsg *msg)
  212. {
  213.   return (struct ScriptContext *) msg -> rm_avail;
  214. }
  215. ///
  216.  
  217. ADDTABL_3(LIBScript_SetStringVar,a0,a1,a2);
  218.  
  219. /// Script_SetStringVar()
  220. LONG LIBRT
  221. LIBScript_SetStringVar (REG(a0) struct ScriptContext *sc, REG(a1) char *name, REG(a2) char *value)
  222. {
  223.   if (SetStringVariable (sc, name, value))
  224.     return 0;
  225.   return 3;
  226. }
  227. ///
  228.  
  229. ADDTABL_3(LIBScript_GetStringVar,a0,a1,a2);
  230.  
  231. /// Script_GetStringVar()
  232. LONG LIBRT
  233. LIBScript_GetStringVar (REG(a0) struct ScriptContext *sc, REG(a1) char *name, REG(a2) char **value)
  234. {
  235.   *value = GetStringVariable (sc, name);
  236.   return 0;
  237. }
  238. ///
  239.  
  240. ADDTABL_END();
  241.